<?php

class FS extends Singletone
{

    /**
     *  The metods starred as und-prefixes
     *          are readers and apperiencers
     *       of value format and data. 
     *
     */
    
    static
    function
    undPath($raw)
    {
        return str_replace('/', I, $raw);
    }
    
    static
    function
    checkPath(&$val)
    {
        $val = self::undPath($val);
        
        /* Cheking the file defined as $val */
        if (file_exists($val))   
        {
            if (is_readable($val)) 
            {
                return true;
            } 
            else 
            {
                throw new TheException("File not readable “{$val}”.");
            }
        } 
        else 
        {
            $msg = print_r(self::getList(self::getParentFolder($val)), true);
            throw new TheException($msg."\n\nFile not exists “{$val}”.");
        }
    }
    
    static
    function
    getParentFolder($path)
    {
        self::undPath($path);
        return dirname($path).I;
    }
    
    static
    function
    checkFolder($path)
    {
        $pathFile = preg_replace('#(/|\\\\)$#su', '', $path);
        
        self::checkPath($pathFile);
        if (is_dir(iconv("UTF-8", "UTF-8", $pathFile)))
        { 
            return true;
        }
        else
        {
            throw new TheException("Invalid folder “{$path}”.");
        }
    }
    
    static
    function
    delete($path)
    {
        self::checkPath($path);
        
        if (is_dir(iconv("UTF-8", "UTF-8", $path))) 
        {
        
        }
        else 
        {
        
        }
        
        if (unlink(iconv("UTF-8", "UTF-8", $path)))
        { 
            return true;
        }
        else
        {
            throw new TheException("Can’t delete a “{$path}”.");
        }
    }
    
    static
    function
    copy($path1, $path2)
    {
        self::checkPath($path1);
        
        self::checkPath($path2);
        
        if (is_dir(iconv("UTF-8", "UTF-8", $path1)))
        { 
            return true;
        }
        else
        {
            throw new TheException("Invalid folder “{$path}”.");
        }
    }
    
    static
    function
    rename($path, $name)
    {
        self::checkPath($path);
        
    }
    
    static
    function
    addFolder($path, $name)
    {
        
    }
    
    static
    function
    create()
    {
    
    }
    
    static
    function
    save()
    {
    
    }
    
    static 
    function
    checkFile($path)
    {
        self::checkPath($path);
        
        if (is_file(iconv("UTF-8", "UTF-8", $path)))
        { 
            return true;
        }
        else
        {
            throw new TheException("Invalid file “{$path}”.");
        }
    }
    
    static
    function
    setNodeInfo($fields, $path)
    {
        self::checkPath($path);
        
        foreach ($fields as $field => $val)
        {  
            switch ($field)
            {
            
            case 'accessTime':
                $tmp = self::getNodeInfo($path);
                touch(iconv("UTF-8", "UTF-8", $path), $tmp['modifyTime'], $val);
                break;
              
            case 'modifyTime':
                touch(iconv("UTF-8", "UTF-8", $path), strtotime($val));
                break;
              
            default:
              
                throw new ArgException(
                  "Can’t setled a property “{$field}” for “{$path}”.");
            }
        }
    }
    
    static
    function
    getList($path)
    { $RES = array();
        
        self::checkFolder($path);
        
        $RES = scandir(iconv("UTF-8", "UTF-8", $path));
        
        return $RES;
    }
    
    static 
    function
    getNodeInfo($path)
    {  $RES = array();
    
        self::undPath($path);
        clearstatcache(true, iconv("UTF-8", "UTF-8", $path));
        self::checkPath($path);

        $RES['path'] = $path;
        
        $pathUTF16 = iconv("UTF-8", "UTF-8", $path);
        
        $RES['isFile'] = is_file($pathUTF16);

        ar::merge(
        
            $RES, 
              
            filterByKeysAndRenameKeys(stat($pathUTF16), // See also lstat/fstat
                array(
                    'ctime' => 'manipulateTime',
                    'mtime' => 'modifyTime',
                    'atime' => 'accessTime',
                    'rdev' => 'devType',
                    'dev' => 'devID',
                    'ino' => 'nodeID',
                    'mode' => 'nodeMode',
                    'nlink' => 'linkNum',
                    'uid' =>  'userID',
                    'gid' =>  'groupID',
                    'size' =>  '',
                )
            )
        );
        
        $RES['is_writable'] = is_writable($pathUTF16);        

        $RES['is_readable'] = is_readable($pathUTF16);        

        $RES['is_executable'] = is_executable($pathUTF16);        

        $RES['is_link'] = is_link($pathUTF16);        

        $RES['fileinode'] = fileinode($pathUTF16);        

        $RES['filegroup'] = filegroup($pathUTF16);        

        $RES['fileowner'] = fileowner($pathUTF16);        

        $RES['filetype'] = filetype($pathUTF16);        

        $RES['fileperms'] = fileperms($pathUTF16);             
        
        $RES['ExternalURLs'] = array(
        
            'HTTP' => array(),
            'FTP' => array(),
            'SVN' => array(),
        );

        return $RES;
    }

}



?>